package org.wzl._360mallbackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.wzl._360mallbackend.dao.CategoryDao;
import org.wzl._360mallbackend.dao.ShoppingCartDao;
import org.wzl._360mallbackend.dto.ShoppingCartDTO;
import org.wzl._360mallbackend.entity.Category;
import org.wzl._360mallbackend.entity.Commodity;
import org.wzl._360mallbackend.entity.ShoppingCart;
import org.wzl._360mallbackend.service.ShoppingCartService;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartDao shoppingCartDao;

    @Autowired
    private CategoryDao categoryDao;

    @Override
    public List<ShoppingCartDTO> getShoppingCartByUserId(Integer userId) {
        List<ShoppingCartDTO> result = new ArrayList<>();
        List<Map<String, Object>> list = shoppingCartDao.getShoppingCartByUserId(userId);
        for (Map<String , Object> map : list) {
            System.out.println(map);
            ShoppingCartDTO shoppingCartDTO = new ShoppingCartDTO();
            Integer id = (Integer) map.get("cart_id");
            shoppingCartDTO.setId(id);
            shoppingCartDTO.setCount((Integer) map.get("count"));
            shoppingCartDTO.setUserId((Integer) map.get("user_id"));
            shoppingCartDTO.setAddTime((LocalDateTime) map.get("add_time"));
            shoppingCartDTO.setCommodityId((Integer) map.get("commodity_id"));
            Commodity commodity = new Commodity();
            commodity.setId((Integer) map.get("commodity_id"));
            commodity.setName((String) map.get("name"));
            commodity.setCategoryId((Integer) map.get("category_id"));
            QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id" , map.get("category_id"));
            Category category = categoryDao.selectOne(queryWrapper);
            commodity.setPrice((BigDecimal) map.get("price"));
            commodity.setImg((String) map.get("img"));
            commodity.setDescImg((String) map.get("desc_img"));
            commodity.setDescription((String) map.get("description"));
            commodity.setRewardPoint((Integer) map.get("reward_point"));
            shoppingCartDTO.setCommodity(commodity);
            shoppingCartDTO.setCategoryName(category.getName());
            result.add(shoppingCartDTO);
        }
        return result;
    }

    @Override
    public void addOneComodityShoppingCartById(Integer commodityId, Integer userId) {
        ShoppingCart userIdAndComId = shoppingCartDao.getCommodityByUserIdAndComId(userId, commodityId);
        if (Objects.isNull(userIdAndComId)) {
            ShoppingCart shoppingCart = new ShoppingCart();
            shoppingCart.setAddTime(LocalDateTime.now());
            shoppingCart.setCount(1);
            shoppingCart.setUserId(userId);
            shoppingCart.setCommodityId(commodityId);
            shoppingCartDao.insert(shoppingCart);
        } else {
            userIdAndComId.setCount(userIdAndComId.getCount() + 1);
            shoppingCartDao.updateById(userIdAndComId);
        }
    }
}
